//
//  HotClassView.m
//  0000327App
//
//  Created by user on 16/3/27.
//  Copyright © 2016年 MoreMoney. All rights reserved.
//
#import "XBHotClassView.h"
#import "XBHotModel.h"
#import "XBHotCollectionViewCell.h"
#import "NSObject+Log.h"

@interface XBHotClassView () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,weak) UICollectionView *collectionView;
@property (nonatomic,weak) UICollectionViewFlowLayout *flowLayout;
@end

@implementation XBHotClassView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

#pragma mark - ======== 加载控件 ========
#define selfWeight self.bounds.size.width
#define selfHeight self.bounds.size.height
- (void)setup
{
    // 设置CollectionView的Size
    CGRect rect = CGRectMake(0, 0, 0, 0);

    // 创建FlowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 创建CollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:flowLayout];
    // 注册cell
    UINib *nib = [UINib nibWithNibName:@"XBHotCollectionViewCell" bundle:nil];
    [collectionView registerNib:nib forCellWithReuseIdentifier:@"hotCollectionCell"];
    // 设置代理、数据源
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.contentSize = self.bounds.size;
    
    self.collectionView = collectionView;
    self.flowLayout = flowLayout;
    
    [self addSubview:collectionView];
    
    collectionView.backgroundColor = [UIColor whiteColor];
}
- (void)layoutSubviews
{
    [super layoutSubviews];

    CGRect rect = CGRectMake(0, 0, selfWeight, selfHeight);
    self.collectionView.frame = rect;
    
    CGFloat col = 4;
    CGFloat row = 3;
    CGFloat weight = selfWeight / col;
    CGFloat height = selfHeight / row;
    
    self.flowLayout.itemSize = CGSizeMake(weight, height);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
}

#pragma mark - ======== 数据源 ========
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 拿到模型
    XBHotModel *model = self.dataArray[indexPath.item];

    XBHotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hotCollectionCell" forIndexPath:indexPath];
    
    cell.model = model;
    
    return cell;
}
#pragma mark - ======== 代理 ========
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    #pragma mark - ======== 点击事件 ========
    // 填写点击事件
    NSLog(@"点击了第%zd个cell",indexPath.item);
}
#pragma mark - ======== 加载数据 ========


#pragma mark - ======== 懒加载 ========
- (NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [XBHotModel models];
    }
    return _dataArray;
}
@end
